|
|
|
|
|
|
|
This code has several important points. First, it does not assign values for pixels along the edge of the picture. If it tried, it would need to apply kernel coefficients to pixels that lie beyond the edges of the picture. Since those pixels are not defined, the program would fail. |
|
|
|
|
|
|
|
|
Second, the subroutine uses separate input_pixels and result_pixels arrays. When it examines a pixel, the code uses pixels with values it has already assigned. For example, to calculate the value of the pixel in position (100, 100), the program examines pixel (99, 99). If it placed new pixel values in the input pixel array, that value would have just been changed. The result for pixel (100, 100) would depend on this new value instead of the original value of pixel (99, 99). |
|
|
|
|
|
|
|
|
Finally, before it assigns a pixel's color components, the program verifies that they lie between 0 and 255. Depending on the input pixel values and the kernel's coefficients, the new component values could lie outside this range. |
|
|
|
|
|
|
|
|
Example program Filter, shown in Figure 7.1, uses this code to show all the spatial filters described in this chapter. It also uses code described later in this chapter to implement other area processing techniques. |
|
|
|
|
|
|
|
|
If you select the Filter menu's Show Filter command, the program displays the current filter's coefficients. The Define Custom Filter command allows you to enter filter coefficients directly. |
|
|
|
|
|
|
|
|
Spatial filtering can take quite a bit of time. Program Filter took roughly 14 seconds to generate the 5 × 5 low-pass filtered image shown in Figure 7.1 on a 133MHz Pentium processor. The output image contains 205 * 289 = 59,245 pixels that need values. For each of these pixels, the program must perform 9 multiplications and 9 additions for a total of 59,245 * (9 + 9) = just over 1 million mathematical operations. With all these calculations, |
|
|
|
|
|
|
|
|
Figure 7.1
Program Filter demonstrates many
different area processing techniques. |
|
|
|
|
|